#if UNITY_EDITOR
using Excel;
using System.Data;
using System.IO;
using Newtonsoft.Json;
using System;
using UnityEngine;
using System.Collections.Generic;
using QFramework;

namespace GGameFramework
{
    public class ExcelUtility
    {
        /// <summary>
        /// 表格数据集合
        /// </summary>
        private static DataSet mResultSet;

        #region ClassCode
        public static string classBase =
    @"
using System;
using System.Collections.Generic;

namespace HareGame
{
 [Serializable]
 public partial class #ClassName#Data:BaseData
 {
   *Data*
 }
}
 ";
        #endregion

        #region ParamCode
        public static string paramBase =
    @"
///<summary>
///#des#
///</summary>
public #type# #name#;

 ";
        #endregion

        #region DesignCode
        public static string DesignBase =
    @"
using System;

namespace HareGame
{
 public partial class #ClassName#Data
 {
   
 }
}
 ";
        #endregion

        #region ManagerCode
        public static string ManagerBase =
    @"
using System;
using System.Collections.Generic;
using UnityEngine;

namespace HareGame
{
    public partial class DataManager
    {
        [SerializeField]
        private List<*Data*> _*class*Datas = new List<*Data*>();

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public *Data* Get*class*Data(*class*ID ID)
        {
            return GetData(ID.ToString(), _*class*Datas);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public *Data* Get*class*Data(string ID)
        {
            return GetData(ID, _*class*Datas);
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public List<*Data*> GetAll*class*Data()
        {
            return _*class*Datas;
        }

        void Init*class*Data()
        {
            _*class*Datas = GameEntry.Util.ReadJsonDataFromStreamAssets<*class*Data>(""*class*"");
        }
    }
}
";
        #endregion

        /// <summary>
        /// 转换为Json
        /// string int float double bool List<T>
        /// </summary>
        public static string ConvertToJson(string excelFile)
        {
            FileStream mStream = File.Open(excelFile, FileMode.Open, FileAccess.Read);
        
            IExcelDataReader mExcelReader = ExcelReaderFactory.CreateOpenXmlReader(mStream);
          
            mResultSet = mExcelReader.AsDataSet();
            
            //判断Excel文件中是否存在数据表
            if (mResultSet.Tables.Count < 1)
                return "";


            string outname = excelFile.Substring(excelFile.LastIndexOf('/') + 1, excelFile.LastIndexOf('.') - excelFile.LastIndexOf('/') - 1);
            //准备一个列表存储整个表的数据
            List<Dictionary<string, object>> table = new List<Dictionary<string, object>>();

            Dictionary<string, string> CheckKey = new Dictionary<string, string>();


            // string classMsg = classBase.Replace("#ClassName#", outname);
            // string classParam = "";

            for (int x = 0; x < mResultSet.Tables.Count; x++)
            {
                //默认读取第一个数据表
                DataTable mSheet = mResultSet.Tables[x];

                //判断数据表内是否存在数据
                if (mSheet.Rows.Count < 3)
                {
                    LogKit.E("<color=blue>" + outname + "</color>中的<color=red>" + mSheet.TableName + "</color>表数据异常！");
                    continue;
                }

                //读取数据表行数和列数
                int rowCount = mSheet.Rows.Count;
                int colCount = mSheet.Columns.Count;

                //读取数据
                for (int i = 3; i < rowCount; i++)
                {
                    //准备一个字典存储每一行的数据
                    var row = new Dictionary<string, object>();
                    bool needAdd = true;
                    for (int j = 0; j < colCount; j++)
                    {
                        //读取第1行数据作为表头字段
                        string field = mSheet.Rows[1][j].ToString();

                        string typestring = mSheet.Rows[2][j].ToString();

                        string valuestr = mSheet.Rows[i][j].ToString();

                        if (j == 0)
                        {
                            if (string.IsNullOrEmpty(valuestr))
                            {
                                needAdd = false;
                                continue;
                            }
                            try
                            {
                                CheckKey.Add(valuestr, mSheet.TableName);
                            }
                            catch
                            {
                                string tableName = CheckKey[valuestr];
                                LogKit.E("<color=#FF00FF>" + outname + "</color>中的<color=red>" + tableName + "</color>表与<color=red>" + mSheet.TableName + "</color>存在相同的ID<color=yellow>" + valuestr + "</color>");
                                needAdd = false;
                                continue;
                            }
                        }

                        // if (outname != "Localization")
                        //     AddParam(mSheet.Rows[0][j].ToString(), typestring, field);

                        //Key-Value对应 按类型存放
                        switch (typestring)
                        {
                            case "int":
                                if (!string.IsNullOrEmpty(valuestr))
                                {
                                    row[field] = Convert.ToInt32(valuestr);
                                }
                                else
                                {
                                    row[field] = 0;
                                }
                                break;
                            case "float":
                                if (!string.IsNullOrEmpty(valuestr))
                                {
                                    row[field] = float.Parse(valuestr);
                                }
                                else
                                {
                                    row[field] = 0;
                                }
                                break;
                            case "double":
                                if (!string.IsNullOrEmpty(valuestr))
                                {
                                    row[field] = double.Parse(valuestr);
                                }
                                else
                                {
                                    row[field] = 0;
                                }
                                break;
                            case "bool":
                                if (valuestr == "0" || valuestr == "false" || valuestr == "")
                                {
                                    row[field] = false;
                                }
                                else
                                {
                                    row[field] = true;
                                }
                                break;
                            case "string":
                                row[field] = valuestr;
                                break;
                            case "List<int>":
                                if (!string.IsNullOrEmpty(valuestr))
                                {
                                    List<int> list = new List<int>();
                                    string[] data = valuestr.Split('#');
                                    foreach (var v in data)
                                    {
                                        list.Add(Convert.ToInt32(v));
                                    }
                                    row[field] = list;
                                }
                                else
                                {
                                    row[field] = new List<int>();
                                }
                                break;
                            case "List<float>":
                                if (!string.IsNullOrEmpty(valuestr))
                                {
                                    List<float> list = new List<float>();
                                    string[] data = valuestr.Split('#');
                                    foreach (var v in data)
                                    {
                                        list.Add(float.Parse(v));
                                    }
                                    row[field] = list;
                                }
                                else
                                {
                                    row[field] = new List<float>();
                                }
                                break;
                            case "List<double>":
                                if (!string.IsNullOrEmpty(valuestr))
                                {
                                    List<double> list = new List<double>();
                                    string[] data = valuestr.Split('#');
                                    foreach (var v in data)
                                    {
                                        list.Add(double.Parse(v));
                                    }
                                    row[field] = list;
                                }
                                else
                                {
                                    row[field] = new List<double>();
                                }
                                break;
                            case "List<string>":
                                if (!string.IsNullOrEmpty(valuestr))
                                {
                                    List<string> list = new List<string>();
                                    string[] data = valuestr.Split('#');
                                    foreach (var v in data)
                                    {
                                        list.Add(v);
                                    }
                                    row[field] = list;
                                }
                                else
                                {
                                    row[field] = new List<string>();
                                }
                                break;
                            case "List<bool>":
                                if (!string.IsNullOrEmpty(valuestr))
                                {
                                    List<bool> list = new List<bool>();
                                    string[] data = valuestr.Split('#');
                                    foreach (var v in data)
                                    {
                                        if (v == "0" || v == "false" || v == "")
                                        {
                                            row[field] = false;
                                        }
                                        else
                                        {
                                            row[field] = true;
                                        }
                                    }
                                    row[field] = list;
                                }
                                else
                                {
                                    row[field] = new List<bool>();
                                }
                                break;
                        }
                        
                    }

                    //添加到表数据中
                    if (needAdd)
                    {
                        table.Add(row);
                    }
                }
            }
            //生成Json字符串
            string json = JsonConvert.SerializeObject(table);
            File.WriteAllText(Application.streamingAssetsPath + "/JsonData/" + outname + ".json", json);

           
            return "Init" + outname + "Data();";
        }
    }
}
#endif